package com.sam4cloud.oauth2.utils;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * @Author: Sam
 * @Date: 2022-12-26 14:24
 * @Description: RSA 非对称加密
 */
public class RSAUtils {

    /**
     * 私钥
     */
    public static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMxDfdpZVkBG0Hwq7eKg/jw9NdkgvU6RZem73KovP5IUDuDjmhF1vL4dDcoYyv7ZavRo0sM8iMc9ltefKFJnz4GB/ch3d+QDisoQFHvqRRE4zk0MW7rwdCcvdO0X+zAec1KIsL/Cw5e9Awr+MGn6SdnCkY5da5HP4X3lEsUGrcqDAgMBAAECgYAO/w+A1dk+u9Q3oYE4h8BSlN3ctIoheAxG5jtzpSdiJqXW/e7ERGQYgxKNmVdh8X1oNz/uBIY4Nn3aE73GmweBZXIgCQ6t1l/8byLd0UtMGO9eL/UHkavpT3hiGRLukeacsoxE3uCJ4KP/fUUysmylHnt7K236+tFtL0I1ugNUIQJBAPLCS1oLUTGq0H+ha0dR4/qMwuo6V369eEf6Vquuc5/MRMopw60wh98lrOojMBwTt8fy39h/PNVvbcoKgnrlnuECQQDXZ61c6fVvG4yJ+A41BT7yyQxj/Nx6EWZxsoepI5m7zZHK7RCB7trq07QmsLq54VjNBGBq82L0n99+ADM1/AnjAkBxlnn9ivgV3lAquBPjTiz3Si/VuzC/a9ZBD6DIOht99O1nDr2xZtjcj9tlvss/B/bhRwJ4YajMUZQ0ilBdQt3hAkAHsBCU51i2HXKXnf3L0KMdgSVrECRCSwqv9iI3XCjpdf3FE07qhD7V8LZWRypy5eMIdiaGyqb2eho84f7x50vnAkEA7vZSDddY7n1RXG48087WpP/xMQVz9Z1s0ZuHu5pUFzKHgZ8OBYo+dFoVvXrWXAkUnBTDnHFy0Q1buXV4h3fvSQ==";

    /**
     * 公钥
     */
    public static final String PUBIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMQ33aWVZARtB8Ku3ioP48PTXZIL1OkWXpu9yqLz+SFA7g45oRdby+HQ3KGMr+2Wr0aNLDPIjHPZbXnyhSZ8+Bgf3Id3fkA4rKEBR76kUROM5NDFu68HQnL3TtF/swHnNSiLC/wsOXvQMK/jBp+knZwpGOXWuRz+F95RLFBq3KgwIDAQAB";

    /**
     * RSA加密算法
     */
    private static final RSA rsa = new RSA(PRIVATE_KEY, PUBIC_KEY);


    /**
     * RSA解密
     *
     * @param data 需要解密的数据
     * @return
     */
    public static byte[] decrypt(String data) {
        return rsa.decrypt(data, KeyType.PublicKey);
    }


    /**
     * RSA解密
     *
     * @param data 需要解密的数据
     * @return
     */
    public static String decryptStr(String data) {
        return rsa.decryptStr(data, KeyType.PublicKey);
    }


    /**
     * RSA加密
     *
     * @param data 需要加密的数据
     * @return
     */
    public static byte[] encrypt(String data) {
        return rsa.encrypt(data, KeyType.PrivateKey);
    }


    /**
     * RSA加密
     *
     * @param data 需要加密的数据
     * @return
     */
    public static String encryptBase64(String data) {
        return rsa.encryptBase64(data, KeyType.PrivateKey);
    }


}
